export default function useMove(vptd, activeElement) {
  /**
   * 初始化鼠标拖拽事件
   * @param {*} e
   */
  function initmovement(e) {
    // 设置移动状态初始值
    vptd.commit("initmove", {
      startX: e.pageX,
      startY: e.pageY,
      originX: activeElement.value.left,
      originY: activeElement.value.top,
    });

    // 绑定鼠标移动事件
    document.addEventListener("mousemove", handlemousemove, true);

    // 取消鼠标移动事件
    document.addEventListener("mouseup", handlemouseup, true);
  }

  /**
   * 鼠标移动
   * @param {*} e
   */
  function handlemousemove(e) {
    e.stopPropagation();
    e.preventDefault();

    vptd.commit("move", {
      x: e.pageX,
      y: e.pageY,
    });
  }

  /**
   * 鼠标up
   */
  function handlemouseup() {
    document.removeEventListener("mousemove", handlemousemove, true);
    document.removeEventListener("mouseup", handlemouseup, true);
    vptd.commit("stopmove");
  }

  return {
    handlemouseup,
    handlemousemove,
    initmovement,
  };
}
